/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import {
  getExpressInfo,
  getOrderPaymentExpressInfo,
  getOrderExpressInfo,
  getGpExpressInfo,
} from "@/services/store";
import { message } from "antd";

export default {
  namespace: "express",

  state: {
    data: null,
    orderExpressInfo: null,
  },

  effects: {
    *getExpressInfo({ payload, __dva_resolve }, { call, put }) {
      const response = yield call(getExpressInfo, payload);
      if (!response) return;
      const { code, msg, data } = response;
      if (code == 0 && data) {
        yield put({
          type: "save",
          payload: {
            expressInfo: data,
          },
        });
      } else {
        message.error(msg);
      }
      __dva_resolve(response);
    },
    // 代收货款，物流详情
    *getOrderPaymentExpressInfo({ payload, __dva_resolve }, { call, put }) {
      const response = yield call(getOrderPaymentExpressInfo, payload);
      if (!response) return;
      const { code, msg, data } = response;
      if (code == 0 && data) {
        yield put({
          type: "save",
          payload: {
            orderExpressInfo: data,
          },
        });
      } else {
        yield put({
          // 防止物流信息显示不正确
          type: "save",
          payload: {
            orderExpressInfo: data,
          },
        });
        message.error(msg);
      }
      __dva_resolve(response);
    },
    // 单号记录物流详情
    *getOrderExpressInfo({ payload, __dva_resolve }, { call, put }) {
      const response = yield call(getOrderExpressInfo, payload);
      if (!response) return;
      const { code, msg, data } = response;
      if (code == 0 && data) {
        yield put({
          type: "save",
          payload: {
            orderExpressInfo: {
              data,
            },
          },
        });
      } else {
        message.error(msg);
      }
      __dva_resolve(response);
    },
    // 共配，获取物流信息
    *getGpExpressInfo({ payload, __dva_resolve }, { call, put }) {
      const response = yield call(getGpExpressInfo, payload);
      if (!response) return;
      const { code, msg, data } = response;
      if (code == 0 && data) {
        yield put({
          type: "save",
          payload: {
            orderExpressInfo: {
              data,
            },
          },
        });
      } else {
        message.error(msg);
      }
      __dva_resolve(response);
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
